import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import svgr from 'vite-plugin-svgr'
import path from 'path'
// 导入 mock 插件
import { viteMockServe } from 'vite-plugin-mock'

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '')

  const isProd = env.VITE_IS_PROD === 'true'
  const isMock = env.VITE_IS_MOCK === 'true'
  
  return {

    plugins: [
      react(), 
      tailwindcss(), 
      svgr({ svgrOptions: { icon: true } }),
      viteMockServe({
        mockPath: 'mock',
        enable: !isProd && isMock,
      }),
    ],
    base: "./",
    server: {
      port: 5683,
      host: '0.0.0.0',
      proxy: !isProd && !isMock
        ? {
            '/index': {
              target: env.VITE_BASE_URL,
              changeOrigin: true,
            }
          }
        : undefined,
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      },
    },
  }
})